/*
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
package commandhandlers;

import java.util.Date;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import main.Constants;
import main.Main;
import botobjects.Admin;
import botobjects.Aid;
import botobjects.Army;
import botobjects.Attack;
import botobjects.Attendance;
import botobjects.Build;
import botobjects.ClaimedTarget;
import botobjects.Command;
import botobjects.Discussion;
import botobjects.Event;
import botobjects.Farm;
import botobjects.HelpTopic;
import botobjects.InstantDamageSpellOp;
import botobjects.Link;
import botobjects.Nap;
import botobjects.NewsItem;
import botobjects.Order;
import botobjects.Post;
import botobjects.Province;
import botobjects.Quote;
import botobjects.Readiness;
import botobjects.SpellOp;
import botobjects.Stats;
import botobjects.Status;
import botobjects.Target;
import botobjects.User;

public class RPCHandler {
   public void addUserCheck(final String nick, final String type) {
	  KDHandler.addUserCheck(nick, type);
   }

   public boolean editPost(final int postID, final String sender, final String editedMessage, final boolean isAdmin) {
	  return Post.editPost(postID, sender, editedMessage, isAdmin);
   }

   public int findFreeGeneral(final String nick) {
	  return ArmyHandler.findFreeGeneral(nick);
   }

   public List<String> findKD(final String query) {
	  return IntelHandler.findKD(query);
   }

   public List<String> findKDByName(final String name) {
	  return IntelHandler.findKDByName(name);
   }

   public List<String> findProvinces(final String query) {
	  return IntelHandler.findProvinces(query);
   }

   public List<Map<String, Object>> getAdmins() {
	  List<Map<String, Object>> out = new LinkedList<Map<String, Object>>();
	  for (Admin admin : Admin.getAdmins()) {
		 out.add(admin.getRPCOutput());
	  }
	  return out;
   }

   public List<Map<String, Object>> getAidList() {
	  List<Map<String, Object>> out = new LinkedList<Map<String, Object>>();
	  for (Aid aid : Aid.getAidList()) {
		 out.add(aid.getRPCOutput());
	  }
	  return out;
   }

   public List<Map<String, Object>> getArmies(final int searchType, final Object... args) {
	  List<Map<String, Object>> out = new LinkedList<Map<String, Object>>();
	  for (Army army : Army.getArmies(searchType, args)) {
		 out.add(army.getRPCOutput());
	  }
	  return out;
   }

   public Map<String, Object> getAttack(final String query) {
	  Attack attack = Attack.getAttack(query);
	  return attack == null ? new HashMap<String, Object>() : attack.getRPCOutput();
   }

   public Map<String, Object> getAttendance(final String description) {
	  Attendance attendance = Attendance.getAttendance(description);
	  return attendance == null ? new HashMap<String, Object>() : attendance.getRPCOutput();
   }

   public Map<String, Object> getBuild(final String race, final String pers, final String type) {
	  return Build.getBuild(race, pers, type).getRPCOutput();
   }

   public List<Map<String, Object>> getBuilds() {
	  List<Map<String, Object>> out = new LinkedList<Map<String, Object>>();
	  for (Build build : Build.getBuilds()) {
		 out.add(build.getRPCOutput());
	  }
	  return out;
   }

   public List<Map<String, Object>> getClaimed() {
	  List<Map<String, Object>> out = new LinkedList<Map<String, Object>>();
	  for (ClaimedTarget target : ClaimedTarget.getClaimed()) {
		 out.add(target.getRPCOutput());
	  }
	  return out;
   }

   public Map<String, Object> getCommand(final String name) {
	  Command command = Command.getCommand(name);
	  return command == null ? new HashMap<String, Object>() : command.getRPCOutput();
   }

   public List<Map<String, Object>> getCommands(final String... args) {
	  List<Map<String, Object>> out = new LinkedList<Map<String, Object>>();
	  for (Command command : Command.getCommands(args)) {
		 out.add(command.getRPCOutput());
	  }
	  return out;
   }

   public List<Map<String, Object>> getDiscussions(final boolean isAdmin) {
	  List<Map<String, Object>> out = new LinkedList<Map<String, Object>>();
	  for (Discussion d : Discussion.getDiscussions(isAdmin)) {
		 out.add(d.getRPCOutput());
	  }
	  return out;
   }

   public List<Map<String, Object>> getEvents() {
	  List<Map<String, Object>> out = new LinkedList<Map<String, Object>>();
	  for (Event event : Event.getEvents()) {
		 out.add(event.getRPCOutput());
	  }
	  return out;
   }

   public List<Map<String, Object>> getFarms() {
	  List<Map<String, Object>> out = new LinkedList<Map<String, Object>>();
	  for (Farm farm : Farm.getFarms()) {
		 out.add(farm.getRPCOutput());
	  }
	  return out;
   }

   public List<Map<String, Object>> getHelp(final String... area) {
	  List<Map<String, Object>> out = new LinkedList<Map<String, Object>>();
	  for (Command command : HelpHandler.getHelp(area)) {
		 out.add(command.getRPCOutput());
	  }
	  return out;
   }

   public Map<String, Object> getHelpTopic(final String topic) {
	  HelpTopic ht = HelpTopic.getHelpTopic(topic);
	  return ht == null ? new HashMap<String, Object>() : ht.getRPCOutput();
   }

   public List<Map<String, Object>> getHelpTopics() {
	  List<Map<String, Object>> out = new LinkedList<Map<String, Object>>();
	  for (HelpTopic topic : HelpTopic.getHelpTopics()) {
		 out.add(topic.getRPCOutput());
	  }
	  return out;
   }

   public List<Map<String, Object>> getInstantDamageSpellOp(final int type, final String... args) {
	  List<Map<String, Object>> out = new LinkedList<Map<String, Object>>();
	  for (InstantDamageSpellOp op : InstantDamageSpellOp.getInstantDamageSpellOp(type, args)) {
		 out.add(op.getRPCOutput());
	  }
	  return out;
   }

   public Map<String, Object> getIntraProvince(final String province, final int... includes) {
	  Province prov = Province.getInternalProvince(province);
	  return prov == null ? new HashMap<String, Object>() : prov.getRPCOutput(includes);
   }

   public List<Map<String, Object>> getIntraProvinces(final int... includes) {
	  List<Map<String, Object>> out = new LinkedList<Map<String, Object>>();
	  for (Province prov : Province.getInternalProvinces()) {
		 out.add(prov.getRPCOutput(includes));
	  }
	  return out;
   }

   public String getLastWarInfo(final String kd, final boolean isWarStart) {
	  return NewsHandler.getLastWarInfo(kd, isWarStart);
   }

   public List<Map<String, Object>> getLinks() {
	  List<Map<String, Object>> out = new LinkedList<Map<String, Object>>();
	  for (Link link : Link.getLinks()) {
		 out.add(link.getRPCOutput());
	  }
	  return out;
   }

   public List<Map<String, Object>> getNaps() {
	  List<Map<String, Object>> out = new LinkedList<Map<String, Object>>();
	  for (Nap nap : Nap.getNaps()) {
		 out.add(nap.getRPCOutput());
	  }
	  return out;
   }

   public List<Map<String, Object>> getNews(final String startDate, final String endDate) {
	  List<Map<String, Object>> out = new LinkedList<Map<String, Object>>();
	  for (NewsItem news : NewsItem.getNews(startDate, endDate, false)) {
		 out.add(news.getRPCOutput());
	  }
	  return out;
   }

   public List<Map<String, Object>> getOrders() {
	  List<Map<String, Object>> out = new LinkedList<Map<String, Object>>();
	  for (Order order : Order.getOrders()) {
		 out.add(order.getRPCOutput());
	  }
	  return out;
   }

   public Map<String, Object> getPlayer(final String nick, final boolean includeContacts) {
	  User user = User.getUser(nick);
	  return user == null ? new HashMap<String, Object>() : user.getRPCOutput(includeContacts ? Constants.USER_INCLUDE_CONTACTS
			: 0);
   }

   public List<Map<String, Object>> getPosts(final int discussionID, final boolean isAdmin) {
	  List<Map<String, Object>> out = new LinkedList<Map<String, Object>>();
	  for (Post post : Post.getPosts(discussionID, isAdmin)) {
		 out.add(post.getRPCOutput());
	  }
	  return out;
   }

   public Map<String, Object> getProvince(final String searchFor, final int... includes) {
	  Province prov = Province.getProvince(searchFor);
	  return prov == null ? new HashMap<String, Object>() : prov.getRPCOutput(includes);
   }

   public List<Map<String, Object>> getProvinces(final String kd, final int... includes) {
	  List<Map<String, Object>> out = new LinkedList<Map<String, Object>>();
	  for (Province prov : Province.getProvinces(kd)) {
		 out.add(prov.getRPCOutput(includes));
	  }
	  return out;
   }

   public List<Map<String, Object>> getQuotes() {
	  List<Map<String, Object>> out = new LinkedList<Map<String, Object>>();
	  for (Quote quote : Quote.getQuotes()) {
		 out.add(quote.getRPCOutput());
	  }
	  return out;
   }

   public Map<String, Object> getReadiness(final String type) {
	  Readiness readiness = Readiness.getReadiness(type);
	  return readiness == null ? new HashMap<String, Object>() : readiness.getRPCOutput();
   }

   public String getReleaseVersion() {
	  return Main.getBotSettings().getReleaseVersion();
   }

   public boolean getShowKD(final String kd) {
	  return KDHandler.getShowKD(kd);
   }

   public List<Map<String, Object>> getSpellOp(final int type, final String... args) {
	  List<Map<String, Object>> out = new LinkedList<Map<String, Object>>();
	  for (SpellOp op : SpellOp.getSpellOp(type, args)) {
		 out.add(op.getRPCOutput());
	  }
	  return out;
   }

   public String getSpellOpTarget(final String user) {
	  return SpellsOpsHandler.getSpellOpTarget(user);
   }

   public List<Map<String, Object>> getStats() {
	  List<Map<String, Object>> out = new LinkedList<Map<String, Object>>();
	  for (Stats stats : Stats.getStats()) {
		 out.add(stats.getRPCOutput());
	  }
	  return out;
   }

   public List<Map<String, Object>> getStatus() {
	  List<Map<String, Object>> out = new LinkedList<Map<String, Object>>();
	  for (Status status : Status.getStatus()) {
		 out.add(status.getRPCOutput());
	  }
	  return out;
   }

   public Map<String, Object> getSummary(final boolean isWar, final String firstDate, final String lastDate, final String targetKD) {
	  return NewsHandler.getSummary(isWar, NewsItem.getNews(firstDate, lastDate, false), targetKD).getRPCOutput();
   }

   public List<Map<String, Object>> getTargets() {
	  List<Map<String, Object>> out = new LinkedList<Map<String, Object>>();
	  for (Target target : Target.getTargets()) {
		 out.add(target.getRPCOutput());
	  }
	  return out;
   }

   public Map<String, Object> getUser(final String nick, final int... includes) {
	  User user = User.getUser(nick);
	  return user == null ? new HashMap<String, Object>() : user.getRPCOutput();
   }

   public Date getUserCheck(final String nick, final String type) {
	  return new Date(KDHandler.getUserCheck(nick, type));
   }

   public List<Map<String, Object>> getUsers() {
	  List<Map<String, Object>> out = new LinkedList<Map<String, Object>>();
	  for (User user : User.getUsers()) {
		 out.add(user.getRPCOutput());
	  }
	  return out;
   }

   public Map<String, Object> getWave() {
	  Event wave = Event.getWave();
	  return wave == null ? new HashMap<String, Object>() : wave.getRPCOutput();
   }

   public boolean lockDiscussion(final int discussionID, final boolean lock) {
	  return Discussion.lockDiscussion(discussionID, lock);
   }

   public void sendSMS(final String user, final String subject, final String content, final String sender) {
	  UserHandler.sendSMS(user, subject, content, sender);
   }

   public void setPassword(final String user, final String password) {
	  UserHandler.setPassword(user, password, false);
   }

   public boolean setShowKD(final String kd, final boolean show) {
	  return KDHandler.setShowKD(kd, show);
   }

   public void setSpellOpTarget(final String nick, final String target) {
	  SpellsOpsHandler.setSpellOpTarget(nick, target);
   }
}